package com.softer.wxzj.service.impl;

import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.flow.util.FlowAddAuditUtil;
import com.softer.wxzj.common.util.StringUtils;
import com.softer.wxzj.mapper.FlowInstanceMapper;
import com.softer.wxzj.service.FlowInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class FlowInstanceServiceImpl implements FlowInstanceService {

    @Autowired
    private FlowAddAuditUtil flowAddAuditUtil;
    @Autowired
    private FlowInstanceMapper flowInstanceMapper;

    @Override
    public CommonResult submit_audit(String businessId, String flowType) {
        log.info("-------------------工作流程初始化开始--------------------");
        //指定业务id和流程类型
        String initState = flowAddAuditUtil.initFlow(businessId, flowType, null);
        log.info("--------------------提交审批工作流程初始化结束------------------");
        return CommonResult.ResponseResult(initState);
    }

    /**
     * 根据业务id检查是否可更新
     *
     * @param businessId
     * @return
     */
    @Override
    public CommonResult checkUpdateCondition(String businessId) {
        int count = flowInstanceMapper.getCountByBusinessId(businessId);
        if (count > 0) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "已存在审核中信息，不允许重复操作！");
        }
        return new CommonResult(ShiroResultEnum.SUCCESS.getCode(), "允许操作");
    }

    @Override
    public CommonResult getListByBusinessId(String businessId) {
        String count = flowInstanceMapper.getListByBusinessId(businessId);
        if (StringUtils.isNotBlank(count)) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "已存在审核中信息" + count + "，不允许重复操作！");
        }
        return new CommonResult(ShiroResultEnum.SUCCESS.getCode(), "允许操作");
    }
}

